/**
 * @see https://leetcode-cn.com/problems/maximum-subarray/
 * 给定一个整数数组 nums ，找到一个具有最大和的连续子数组（子数组最少包含一个元素），返回其最大和
 * 动态规划解法
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function (nums) {
  let i = 0;
  let l = nums.length - 1;
  let f = [];
  // 当只有最长序列为n时的最大和 f(n) = max([f(n-1)+vn, vn])
  for (let i = 0; i < nums.length; i++) {
    const element = nums[i];
    f[i] = Math.max((f[i - 1] || 0) + element, element);
  }
  console.log(Math.max.apply(null, f));
};

maxSubArray([-2,1,-3,4,-1,2,1,-5,4,10]);
maxSubArray([1]);